package com.aliyun.frontclient;

import com.aliyun.frontframework.NewFrontCamera;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Process;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.content.ComponentName;

public class FrontClientSurfaceViewListener implements SurfaceHolder.Callback,
        IFrontClientSurfaceCallback {

    // TODO 服务端修正后可自动识别窗口大小，无需显示传入
    private static final String TAG = "FCSVL";
    private Context mContext;
    private View mSurfaceView;

    // XXX
    // 改成以hashString区别的话，无法兼容多个SurfaceView，而目前这个无法兼容多个SurfaceTextue构建的Surface
    // 标识后置摄像头的显示区域，为避免无法区分不要每次创新新的
    protected Surface mBackSurface;

    public FrontClientSurfaceViewListener(Context cnt) {
        init(cnt, null);
    }

    public FrontClientSurfaceViewListener(View surfaceView) {
        init(surfaceView.getContext(), surfaceView);
    }

    // @Override
    protected void init(Context cnt, View surfaceView) {
        // System.out.println( " pkgName:" + cnt.getPackageName() );
        mContext = cnt.getApplicationContext();
        // System.out.println( " pkgName:" + mContext.getPackageName() );
        setSurfaceViewWH(surfaceView);
    }

    public void setSurfaceViewWH(View surfaceView) {
        this.mSurfaceView = surfaceView;
    }

    protected void setBackSurface(Surface surface) {
        println("-setBackSurface-" + surface);
        this.mBackSurface = surface;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        surfaceCreated(holder.getSurface());
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        surfaceChanged(holder.getSurface(), format, width, height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        surfaceDestroyed(holder.getSurface());
    }

    private Intent genrateIntent(Surface surface, String method, int format,
            int width, int height) {
        // Intent intent= new Intent( mContext , BackRecordService.class );
        // new Intent( "cn.spt.action.TEST_SURFACE_REC" );
        // intent.setPackage( "com.example.testvideo" );
        if(NewFrontCamera.DEBUG_SURFACE) Log.v(TAG, "genrateIntent " + method + " surface " + surface.hashCode());
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(
                "com.example.androidsurfacegarafika",
                "com.aliyun.frontserver.CoreService"));
        intent.putExtra("behavior", "surfaceAction");
        intent.putExtra("action", method);
        intent.putExtra("surface", surface);
        intent.putExtra("code", surface.toString());
        intent.putExtra("format", format);
        intent.putExtra("width", width);
        intent.putExtra("height", height);
        return intent;
    }

    private void sendIntent(Surface surface, String method) {
        sendIntent(surface, method, 0, 0);
    }

    private void sendIntent(Surface surface, String method, int width,
            int height) {
        sendIntent(surface, method, PixelFormat.RGB_565, width, height);
    }

    private void sendIntent(Surface surface, String method, int format,
            int width, int height) {
        // mContext.sendBroadcast( genrateIntent(surface, action, format, width,
        // height) );
        mContext.startService(genrateIntent(surface, method, format, width,
                height));
    }

    @Override
    public void surfaceCreated(Surface surface) {
        int w = 0, h = 0;
        View v = mSurfaceView;
        if (v != null) {
            w = v.getWidth();
            h = v.getHeight();
        }
        surfaceCreated(surface, w, h);
    }

    protected void surfaceCreated(Surface surface, int width, int height) {
        println("-surfaceCreated-" + surface + "," + "," + width + "*" + height);

        sendIntent(surface, "surfaceCreated", width, height);
    }

    @Override
    public void surfaceChanged(Surface surface, int format, int width,
            int height) {
        println("-surfaceChanged-" + surface + "," + format + "," + width + "*"
                + height);
        sendIntent(surface, "surfaceChanged", format, width, height);
    }

    @Override
    public void surfaceDestroyed(Surface surface) {
        println("-surfaceDestroyed-" + surface);
        sendIntent(surface, "surfaceDestroyed");
    }

    static void println(Object o) {
        Log.d(TAG, "pid:" + Process.myPid() + ", " + Thread.currentThread()
                + " : " + String.valueOf(o));
    }

}
